package com.business.section.validator;

import com.common.MyCaptchaRender;
import com.common.util.picture.PictureBase64;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
import com.model.Section;

public class SectionValidator extends Validator {

	@Override
	protected void validate(Controller c) {
		c.getFile("contextFile");
		//校验标题是否为空
		String name=c.getPara("section.name").trim();
		if(name==null || "".equals(name.trim()))
		{
			addError("nameMsg", "标题不能为空");
		}
		
		//内容
		String context=c.getPara("section.context");
		if(context==null || "".equals(context.trim()))
		{
			addError("contextMsg", "内容不能为空");
		}
		
		String text = PictureBase64.removeHtmlInTag(context);
		if(text==null || "".equals(text.trim())|| text.length()<5){
			addError("contextMsg", "不能少于5个字");
		}
		
		//校验验证码
		String captcha = c.getPara("captcha");
		boolean vCodeCheck=MyCaptchaRender.validate(c, captcha.trim());
		if (captcha == null || "".equals(captcha.trim())) {
			addError("codeMsg", "验证码不能为空");
		} else if (!vCodeCheck) {
			addError("codeMsg", "验证码错误");
		}
	}

	// 验证validate内容出现错误则执行handleError
	@Override
	protected void handleError(Controller c) {
		c.keepModel(Section.class);
		c.render("add.html");
	}

}